package com.wetao.module_homework.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.wetao.lib_common.base.BaseListResponse;
import com.wetao.lib_common.base.BaseResponse;
import com.wetao.lib_common.bean.response.CommitAssistantAnswerResp;
import com.wetao.lib_common.bean.response.GetAssistantQuestionList;
import com.wetao.lib_common.bean.response.GetSubjectExamNumber;
import com.wetao.lib_common.bean.response.Loginbean;
import com.wetao.lib_common.bean.response.QueryExamQuestionsResponse;
import com.wetao.lib_common.bean.response.StudentExamListInfo;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.GsonUtil;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.utils.ToastUtil;
import com.wetao.module_homework.R;
import com.wetao.module_homework.contract.HomeworkLineActivityCon;
import com.wetao.module_homework.contract.HomeworkMainFragmentCon;

import java.util.HashMap;

import okhttp3.RequestBody;


/**
 * author: zhouyh
 * created on: 2019-11-15 16:33
 * description:
 */
public class HomeworkLineActivityPre extends HomeworkLineActivityCon.Presenter {


    private Context mContext;

    public HomeworkLineActivityPre(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void getQuestionList(HashMap<String, Object> map, boolean isDialog, boolean isCancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().questionList(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse<QueryExamQuestionsResponse>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<QueryExamQuestionsResponse>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<QueryExamQuestionsResponse>>(mContext, RxErrorHandler.getInstance(),isDialog,isCancelable) {
                    @Override
                    public void onNext(BaseResponse<QueryExamQuestionsResponse> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            if (getView()!=null){
                                getView().getQuestionListResult(baseResponse);
                            }
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            if (getView()!=null){
                                getView().getQuestionListFailed();
                            }
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(mContext.getString(R.string.network_disconnect));
                        if (getView()!=null){
                            getView().getQuestionListFailed();
                        }
                    }
                });
    }

    @Override
    public void courseExamQuestionList(HashMap<String, Object> map, boolean isDialog, boolean isCancelable) {
        if (getView()==null)return;
        String json = GsonUtil.GsonString(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().courseExamQuestionList(requestBody)
                .retryWhen(new RetryWithDelay(3,2))
                .compose(RxUtils.<BaseResponse<QueryExamQuestionsResponse>>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse<QueryExamQuestionsResponse>>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse<QueryExamQuestionsResponse>>(mContext, RxErrorHandler.getInstance(),isDialog,isCancelable) {
                    @Override
                    public void onNext(BaseResponse<QueryExamQuestionsResponse> baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            if (getView()!=null){
                                getView().getQuestionListResult(baseResponse);
                            }
                        }else{
                            ToastUtil.initToast(baseResponse.getMsg());
                            if (getView()!=null){
                                getView().getQuestionListFailed();
                            }
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        ToastUtil.initToast(mContext.getString(R.string.network_disconnect));
                        if (getView()!=null){
                            getView().getQuestionListFailed();
                        }
                    }
                });
    }

    @Override
    public void getCommitAnswer(String json, boolean isDialog, String tip, boolean isCancelable) {
        if (getView()==null)return;
        LogUtils.d("req json = " + json);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().commitAnswer(requestBody)
            .retryWhen(new RetryWithDelay(3,2))
            .compose(RxUtils.<BaseResponse<CommitAssistantAnswerResp>>bindToLifecycle(getView()))
            .compose(RxUtils.<BaseResponse<CommitAssistantAnswerResp>>getSchedulerTransformer())
            .subscribe(new RequestCallback<BaseResponse<CommitAssistantAnswerResp>>(mContext, RxErrorHandler.getInstance(),isDialog, tip, isCancelable) {
                @Override
                public void onNext(BaseResponse<CommitAssistantAnswerResp> baseResponse) {
                    super.onNext(baseResponse);
                    if(baseResponse.getCode() == 0){
                        if (getView()!=null){
                            getView().getCommitAnswerResult(baseResponse);
                        }
                    }else{
                        ToastUtil.initToast(baseResponse.getMsg());
                        if (getView()!=null){
                            getView().getCommitAnswerFailed();
                        }
                    }
                }
                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    //ToastUtil.initToast(mContext.getString(R.string.network_disconnect));
                    if (getView()!=null){
                        getView().getCommitAnswerFailed();
                    }
                }
            });
    }


    @Override
    public void queryClassroomTestQuestionList(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json =(new Gson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().queryClassroomTestQuestionOnlineList(requestBody)
            .retryWhen(new RetryWithDelay(3,2))
            .compose(RxUtils.<BaseResponse<QueryExamQuestionsResponse>>bindToLifecycle(getView()))
            .compose(RxUtils.<BaseResponse<QueryExamQuestionsResponse>>getSchedulerTransformer())
            .subscribe(new RequestCallback<BaseResponse<QueryExamQuestionsResponse>>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                @Override
                public void onNext(BaseResponse<QueryExamQuestionsResponse> baseResponse) {
                    super.onNext(baseResponse);
                    if(baseResponse.getCode() == 0){
                        if (getView()!=null){
                            getView().queryClassroomTestQuestionListResult(baseResponse);
                        }
                    }else{
                        ToastUtil.initToast(baseResponse.getMsg());
                        if (getView()!=null){
                            getView().queryClassroomTestQuestionListFailed();
                        }
                    }
                }
                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                    ToastUtil.initToast(e.getMessage());
                    if (getView()!=null){
                        getView().queryClassroomTestQuestionListFailed();
                    }
                }
            });
    }

}
